
package org.markos.util;

import java.io.Serializable;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.markos.model.Company;
import org.markos.model.Product;

/**
 * TODO Place information about the class or interface here
 * 
 * @author fragkakm
 */
@Name("rowspan")
@Scope(ScopeType.SESSION)
@AutoCreate
public class RowspanCalc implements Serializable {

	public int calcTotalRows(List<Company> companies) {

		int result = 0;
		for (Company comp : companies) {
			result += calcCompanyRowspan(comp);
		}
		System.out.println("Total Rows " + result);
		return result;
	}

	/**
	 * Calculate for Product
	 * 
	 * @param company
	 * @return
	 */
	public int calcCompanyRowspan(Company company) {

		List<Product> prodList = company.getProducts();
		if (prodList.size() > 0) {
			int rowspan = 0;
			for (Product prod : prodList) {
				rowspan += calcProductRowspan(prod);
			}
			System.out.println("Company " + company.getName() + " " + rowspan);
			return rowspan;
		} else {
			System.out.println("Company " + company.getName() + " " + 1);
			return 1;
		}
	}

	/**
	 * Calculate for product
	 * 
	 * @param product
	 * @return
	 */
	public int calcProductRowspan(Product product) {

		int result;
		if (product.getVersions().size() > 0) {
			result = product.getVersions().size();
		} else {
			result = 1;
		}
		System.out.println("Product " + product.getName() + " " + result);
		return result;
	}
}
